package com.woniuxy.educationplatform.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.service.IOptService;
import com.woniuxy.educationplatform.service.IOrderService;
import com.woniuxy.educationplatform.service.IRefoundService;
import com.woniuxy.educationplatform.service.ITransportService;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Resource
    private IOrderService orderServiceImpl;
    @Resource
    private IRefoundService refoundService;
    @Resource
    private IOptService optService;
    @Resource
    private ITransportService transportService;

    @RequestMapping("/page")
    public List<OrderBean> page(int index,int nums){
        return orderServiceImpl.findByPage(index,nums);
    }
    @RequestMapping(value = "/details",produces = {"application/json;charset=utf-8"})
    public OrderBean details(Long id){
        OrderBean o = orderServiceImpl.findById(id);
        System.out.println(o);
        return o;
    }

    @RequestMapping(value = "/count",produces = {"application/json;charset=utf-8"})
    public int count(){
        return orderServiceImpl.count();
    }


    @PostMapping(value = "/find", produces = {"application/json;charset=utf-8"})
    public List<OrderBean> find(@RequestBody OrderBean orderBean){
        System.out.println(orderBean);
        List<OrderBean> os = orderServiceImpl.findByCondition(orderBean);
        return os;
    }

    @RequestMapping(value = "/findGoods", produces = {"application/json;charset=utf-8"})
    public Map<String,String> findByOrderId(Long id) throws JsonProcessingException {
        List<GoodsBean> goods = orderServiceImpl.findByOrderId(id);
        String goodsBean  = new ObjectMapper().writeValueAsString(goods);
        int[] numss = orderServiceImpl.findNumsByOrderId(id);
        String nums = new ObjectMapper().writeValueAsString(numss);
        Map<String,String> map = new HashMap<>();
        map.put("goods",goodsBean);
        map.put("nums",nums);
        return map;
    }

    @RequestMapping(value = "/findRefund", produces = {"application/json;charset=utf-8"})
    public RefundBean findRFByOrderId(Long id){
        return refoundService.finfByOrderId(id);
    }

    @RequestMapping(value = "/findOpt", produces = {"application/json;charset=utf-8"})
    public List<OptBean> findOpts(Long id){
        return optService.findByOrderId(id);
    }

    @RequestMapping(value = "changeMoney",produces = {"application/json;charset=utf-8"})
    public void changeMoney(Long id,Double money){
        orderServiceImpl.changeMoney(id,money);
        //新建操作
        OptBean optBean = new OptBean();
        OrderBean orderBean = new OrderBean();
        orderBean.setId(id);
        optBean.setOrderBean(orderBean);
        optBean.setOrderStatus(7);
        optBean.setOrderSend(1);
        optBean.setOrderPay(0);
        optBean.setType(2);
        optService.add(optBean);
    }

    @RequestMapping(value = "send",produces = {"application/json;charset=utf-8"})
    public void send(Long id,String no){
        //新建快递
        TransportBean t = new TransportBean();
        t.setPostCode("000000");
        t.setTransNo(no);
        transportService.add(t);

        OptBean optBean = new OptBean();

        //修改订单
        OrderBean orderBean = new OrderBean();
        orderBean.setId(id);
        orderBean.setStatus(7);
        orderBean.setTransportBean(t);
        orderServiceImpl.update(orderBean);

        optBean.setOrderBean(orderBean);
        optBean.setOrderStatus(7);
        optBean.setOrderSend(1);
        optBean.setOrderPay(0);
        optBean.setType(1);
        optService.add(optBean);
    }

    @RequestMapping(value ="updateOrder",produces = {"application/json;charset=utf-8"})
    public void update(@RequestBody OrderBean orderBean){
        orderServiceImpl.update(orderBean);
        //新建一个操作bean
        OptBean optBean = new OptBean();
        OrderBean thisOrder = orderServiceImpl.findById(orderBean.getId());
        optBean.setOrderBean(thisOrder);
        optBean.setOrderPay(1);
        optBean.setOrderSend(1);
        optBean.setOrderStatus(thisOrder.getStatus());
        optService.add(optBean);
    }
}
